package es.uji.viselab.experiment;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.jdesktop.swingx.combobox.EnumComboBoxModel;

import es.uji.viselab.benchmark.DataPackage;
import es.uji.viselab.experiment.control.ExperimentCatalog;
import es.uji.viselab.image.source.ImageSourceCatalog;

public class ExperimentCustomizable extends ExperimentInterface implements ItemListener {

	protected Label labelConfiguration = new Label("Control Loop ");
	protected JComboBox availableConfiguration;

	protected Button buttonStart = new Button("Start");
	protected JPanel rootPanel;

	public ExperimentCustomizable(ExperimentParameters ep) {
		super(ep);
		constructPanel();
	}

	protected void constructPanel() {

		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));

		availableConfiguration = new JComboBox();
		availableConfiguration.setModel(new EnumComboBoxModel<ExperimentCatalog>(
				ExperimentCatalog.class));
		if (ep.getExperiment() == null) {
			availableConfiguration.setSelectedItem(ExperimentCatalog.CONTROLLOOP_POS200);
			ep.setExperiment(ExperimentCatalog.CONTROLLOOP_POS200);
		} else
			availableConfiguration.setSelectedItem(ep.getExperiment());

		JPanel line1 = new JPanel();
		line1.setLayout(new FlowLayout());
		((FlowLayout) line1.getLayout()).setAlignment(FlowLayout.CENTER);

		line1.add(labelConfiguration);
		line1.add(availableConfiguration);

		JPanel line2 = new JPanel();
		line2.setLayout(new FlowLayout());
		((FlowLayout) line2.getLayout()).setAlignment(FlowLayout.CENTER);

		JPanel line3 = new JPanel();
		line3.setLayout(new FlowLayout());
		((FlowLayout) line2.getLayout()).setAlignment(FlowLayout.CENTER);
		line3.add(buttonStart);

		buttonStart.addActionListener(this);
		
		availableConfiguration.addItemListener(this);

		rootPanel.add(line1);
		rootPanel.add(line2);
		rootPanel.add(line3);

	}

	public JPanel getPanel() {
		return rootPanel;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(buttonStart)) {
			start();
		}
	}
	
	
	@Override
	public void start() {
		initializeComponents();
		registerNewExperiment();
		experiment.start();
	}

	@Override
	public String getPanelTitle() {
		return "Experiment customizable (simple parameters)";
	}

	@Override
	public int panelWidth() {
		return 500;
	}

	@Override
	public int panelHeight() {
		return 200;
	}

	@Override
	public void newDataArrived(DataPackage dn) {
	}

	@Override
	public void itemStateChanged(ItemEvent ie) {
		if (ie.getSource().equals(availableConfiguration)) {
		ExperimentCatalog experiment = (ExperimentCatalog) availableConfiguration.getSelectedItem();
		ep.setExperiment(experiment);
		}
	}

}
